package com.jinlia.show.modules.base.system.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.modules.base.system.entity.SysAreaCode;
import com.jinlia.show.modules.base.system.mapper.AreaCodeMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class AreaCodeMapperService extends ServiceImpl<AreaCodeMapper, SysAreaCode> {

    public List<SysAreaCode> getList(SysAreaCode areaCode){
        LambdaQueryChainWrapper<SysAreaCode> lambdaQuery = this.lambdaQuery();
        List<SysAreaCode> list;
        if (Objects.isNull(areaCode.getCode())) {//查询省份
            list = lambdaQuery.like(Objects.isNull(areaCode.getCode()), SysAreaCode::getCode, "%0000").list();
        } else if (areaCode.getCode() % 10000 == 0) { //查询城市
            list = lambdaQuery.apply("code like {0}", String.format("%s%%00", (areaCode.getCode() / 10000))).list();
        } else {//查询地区
            list = lambdaQuery.likeRight(SysAreaCode::getCode, String.format("%s", (areaCode.getCode() / 100))).list();
        }
        //去除自己
        list = list.stream().filter(i->!i.getCode().equals(areaCode.getCode())).collect(Collectors.toList());
        return list;
    }
}
